package code.phpnet
{
	import com.adobe.crypto.MD5;
	
	import flash.events.ErrorEvent;
	import flash.events.EventDispatcher;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;

	/**
	 *与web通信的基类 
	 * @author YY
	 * 
	 */
	public class PhpServer
	{
		protected var oConnect:NetConnection;
		protected var _globalParam:GlobalConfig = GlobalConfig.instance;//网页传过来的参数

		public function PhpServer()
		{
			this.oConnect = new NetConnection;
			this.oConnect.objectEncoding = ObjectEncoding.AMF3;
			this.oConnect.addEventListener(NetStatusEvent.NET_STATUS, connectionNetStatusHandler);		
			this.oConnect.connect(_globalParam.gateway);
		}
		
		private function connectionNetStatusHandler(event:NetStatusEvent):void 
		{
			for (var x in event.info) 
			{
				trace( event.info[x]);
			}			
			
		}
		
		protected function serverFault(info:*) 
		{
			for (var x in info)
			{
				trace( x + '-->' + info[x]);
			}			
		}
		
		/**
		 * 剥离从服务器端返回的数据
		 * @param rets
		 */
		protected function getRet( rets:*) {			
			if ( typeof(rets['time']) != 'undefined') {//时间校准
				_globalParam.adtime = int((new Date()).valueOf()/1000) - int(rets['time']);
			}
			if ( typeof(rets['flag']) != 'undefined') {//服务器端标志
				
			}
			if ( typeof(rets['ret']) != 'undefined') {
				return rets['ret'];
			}
		}
		/**
		 * 生成完整的带签名的参数
		 * @param param
		 * @param auth 是否验证,是验证则重新启动定时器
		 */
		function genParam( param:*) {
			var ret:Array = new Array();
			ret["uid"] = _globalParam.mid;    //用户ID
			ret['pfid'] = _globalParam.sid;  //用户的平台ID,比如人人1,漫游2
			ret['usid'] = _globalParam.unid; //用户的子ID,比如城市ID,大学ID,站点子ID等
			ret['param'] = param;            //此次请求的真正数据,数组
			ret['olkey'] = _globalParam.mtkey;//用户的私有key
			ret['lang'] = _globalParam.langtype;//语言标志
			ret['time'] = int((new Date()).valueOf() / 1000 - int(_globalParam.adtime)).toString();//发起请求的时间
			ret['sign'] = MD5.hash( joins( ret, _globalParam.mtkey));			
			return ret;
		}
		
		
	}
}
include "./protected.as"